<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受灾街道" prop="town">
        <el-select v-model="queryParams.town" placeholder="请选择受灾街道" clearable>
          <el-option
            v-for="dict in town"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="街道详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入街道详细地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警级别" prop="warnLevel">
        <el-select v-model="queryParams.warnLevel" placeholder="请选择预警级别" clearable>
          <el-option
            v-for="dict in warn_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeWarnTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="灾害类型" prop="warnType">
        <el-select v-model="queryParams.warnType" placeholder="请选择灾害类型" clearable>
          <el-option
            v-for="dict in warn_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入负责人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['town:warn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['town:warn:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['town:warn:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['town:warn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="uname" />
      <el-table-column label="受灾街道" align="center" prop="town">
        <template #default="scope">
          <dict-tag :options="town" :value="scope.row.town"/>
        </template>
      </el-table-column>
      <el-table-column label="街道详细地址" align="center" prop="address" />
      <el-table-column label="预警级别" align="center" prop="warnLevel">
        <template #default="scope">
          <dict-tag :options="warn_level" :value="scope.row.warnLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="预警时间" align="center" prop="warnTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="灾害类型" align="center" prop="warnType">
        <template #default="scope">
          <dict-tag :options="warn_type" :value="scope.row.warnType"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="负责人电话" align="center" prop="phone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['town:warn:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['town:warn:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改灾害预警对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="warnRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="受灾街道" prop="town">
          <el-select v-model="form.town" placeholder="请选择受灾街道">
            <el-option
              v-for="dict in town"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="街道详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入街道详细地址" />
        </el-form-item>
        <el-form-item label="预警级别" prop="warnLevel">
          <el-select v-model="form.warnLevel" placeholder="请选择预警级别">
            <el-option
              v-for="dict in warn_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预警时间" prop="warnTime">
          <el-date-picker clearable
            v-model="form.warnTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择预警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="灾害类型" prop="warnType">
          <el-select v-model="form.warnType" placeholder="请选择灾害类型">
            <el-option
              v-for="dict in warn_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="预防措施" prop="remake">
          <el-input v-model="form.remake" type="textarea" placeholder="请输入预防措施" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Warn">
import { listWarn, getWarn, delWarn, addWarn, updateWarn } from "@/api/town/warn";

const { proxy } = getCurrentInstance();
const { warn_level, town, warn_type } = proxy.useDict('warn_level', 'town', 'warn_type');

const warnList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeWarnTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createTime: null,
    uname: null,
    town: null,
    address: null,
    warnLevel: null,
    warnTime: null,
    warnType: null,
    leader: null,
    phone: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询灾害预警列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeWarnTime && '' != daterangeWarnTime) {
    queryParams.value.params["beginWarnTime"] = daterangeWarnTime.value[0];
    queryParams.value.params["endWarnTime"] = daterangeWarnTime.value[1];
  }
  listWarn(queryParams.value).then(response => {
    warnList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    relevanceId: null,
    createTime: null,
    updateTime: null,
    uid: null,
    uname: null,
    town: null,
    address: null,
    warnLevel: null,
    warnTime: null,
    warnType: null,
    leader: null,
    phone: null,
    remake: null
  };
  proxy.resetForm("warnRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeWarnTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加灾害预警";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWarn(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改灾害预警";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warnRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarn(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarn(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除灾害预警编号为"' + _ids + '"的数据项？').then(function() {
    return delWarn(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('town/warn/export', {
    ...queryParams.value
  }, `warn_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style lang="scss" scoped>
:deep(.el-form-item__label) {
  display: block;
}

:deep(.el-select.el-select--default) {
  width: 100%;
}

:deep(.el-form-item.el-form-item--default.asterisk-left.label-width label) {
  padding-left: 10px;
}
:deep(
    .el-input.el-input--default.el-input--prefix.el-input--suffix.el-date-editor.el-date-editor--date.el-tooltip__trigger.el-tooltip__trigger
  ) {
  width: 100%;
}

.custom-form {
  max-width: 100%;
  border: 1px solid #ccc;
  padding: 1rem 4rem 0;
}

.form-row {
  margin-bottom: 20px;
}

.custom-divider {
  border-style: dashed; /* 修改边框样式为虚线 */
  border-width: 2px; /* 设置边框宽度 */
  border-color: #ccc; /* 设置边框颜色 */
}
</style>